package toolkit.tianshi.excelpoi.serviceimpl;

import com.google.common.collect.Lists;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import toolkit.tianshi.common.excelpoi.AnalyzeModel;
import toolkit.tianshi.excelpoi.service.AnalyzeService;


/**
 * 用于解析excel表中的数据
 * @author zhangLin
 */
public class AnalyzeServiceImpl implements AnalyzeService {

    /**
     * 读取当前excel中的数据分析模板
     * filepath 文件路径
     */
    @Override
    public AnalyzeModel analyseExcel(String filepath) throws Exception{
        InputStream inp  = new FileInputStream(filepath);
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        AnalyzeModel analyzeModel = new AnalyzeModel();
        List<AnalyzeModel.Site> tableTitle = Lists.newArrayList();
        List<AnalyzeModel.Site> content = Lists.newArrayList();
        AnalyzeModel.Site site;
        Integer initialRow = null;
        for (Row row : sheet) {
            String originalString = null;
            for (Cell cell : row) {
                switch (cell.getCellTypeEnum()) {
                    case STRING:
                        originalString = cell.getRichStringCellValue().getString();
                        break;
                    case NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                            originalString = String.valueOf(cell.getDateCellValue());
                        } else {
                            originalString = String.valueOf(cell.getNumericCellValue());
                        }
                        break;
                    case BOOLEAN:
                        originalString = String.valueOf(cell.getBooleanCellValue());
                        break;
                    case FORMULA:
                        originalString = String.valueOf(cell.getCellFormula());
                        break;
                    case BLANK:
                        break;
                    default:
                }
                if (originalString != null) {
                    if (originalString.startsWith("$.{")){
                        site = new AnalyzeModel.Site();
                        site.setRow(row.getRowNum());
                        site.setColumn(cell.getColumnIndex());
                        site.setContent(
                                originalString.substring(
                                        originalString.indexOf("$.{")+3,originalString.indexOf("}"))
                        );
                        tableTitle.add(site);
                    } else if (originalString.startsWith("o.{")){
                        site = new AnalyzeModel.Site();
                        initialRow = row.getRowNum();
                        site.setColumn(cell.getColumnIndex());
                        site.setContent(
                                originalString.substring(
                                        originalString.indexOf("o.{")+3,originalString.indexOf("}"))
                        );
                        content.add(site);
                    }
                }
            }
            System.out.println();
        }

        analyzeModel.setContent(content);
        analyzeModel.setInitialRow(initialRow);
        analyzeModel.setTableTitle(tableTitle);
        return analyzeModel;
    }

}
